/**
 * Created by Lcp on 2018/5/29.
 */
var fileArray = [],//防止重复上传
    fileData = [],//上传数据
    tableData = [];//表单数据

$('textarea').css({height: $('.contents').height() - 150 +　'px'});
window.onresize = function(){
    $('textarea').css({height: $('.contents').height() - 150 +　'px'});
}
function getImgURL(node){
    if(node.files && node.files[0] ){
        file = node.files[0];
    }else if(node.files && node.files.item(0)) {
        file = node.files.item(0);
    }
    if (fileArray.indexOf(file.name) >= 0){
        Msg('您已选择，如需重复选择请修改名称。');
    } else {
        tableData.push({
            name: file.name.substring(0,file.name.lastIndexOf('.')),
            size: file.size,
            address: file.type,
            edit: '<a class="layui-btn layui-btn-danger layui-btn-md" lay-event="del" onclick="del('+ fileArray.length +')")>删除</a>'
        });
        fileArray.push(file.name);
        fileData.push(file);
        buildTable();
    }
    $(node).val('');
}
//建立表格
function buildTable(){
    layui.use('table', function(){
        var table = layui.table;
        //展示已知数据
        table.render({
            elem: '#test',
            cols: [[ //标题栏
                {field: 'name', title: '名称', width: '25%', sort: true},
                {field: 'size', title: '大小（kb）', width: '25%', sort: true},
                {field: 'address', title: '类型', width: '25%'},
                {field: 'edit', title: '编辑', width: '25%'}
            ]],
            data: tableData,
            even: true
        });
    });
}
function del(Index){
    console.log(Index);
    layer.msg('确定删除？', {
        time: 0, //不自动关闭
        btn: ['确定', '取消'],
        yes: function(index){
            layer.close(index);
            fileArray.splice(Index,1);
            fileData.splice(Index,1);
            tableData.splice(Index,1);
            buildTable();
        }
    });
}
function uploadMsg(item){
    if(tableData.length > 0){
        var formData = new FormData();
        fileData.forEach(function(item,index){
            formData.append('file',item);
        })
        $.ajax({
            type: 'post',
            url: baseUrls + 'savetools',
            cache: false,
            contentType: false,
            processData: false,
            data: formData,
            beforeSend: function(){
                Loading();
            },
            success: function(response){
                closeLoading();
                if(response.responseCode == '1000'){
                    Msg('附件上传成功',1);
                    fileArray = [];
                    fileData = [];
                    tableData = [];
                    buildTable();
                }
            },
            error: function(){
                closeLoading();
                Msg();
            }
        })
    }else{
        Msg('请选择附件');
    }
}
function saveFile(item){
    if($.trim($('.name').val()).length > 0){
        if($.trim($('.text').val()).length > 0){
            $.ajax({
                type: 'post',
                url: baseUrls + 'automation/notice',
                cache: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify({
                    mc: $.trim($('.name').val()),
                    fbr: $('#userCode').val(),
                    ms: $.trim($('.text').val())
                }),
                beforeSend: function(){
                    Loading();
                },
                success: function(response){
                    closeLoading();
                    if(response.responseCode == '1000'){
                        Msg('通知通告上传成功',1);
                        $('.name').val('');
                        $('.text').val('');
                    }
                },
                error: function(){
                    closeLoading();
                    Msg();
                }
            })
        }else{
            Msg('请填写正文',5);
        }
    }else{
        Msg('请填写用户名',5);
    }
}
function getvl(obj){
//判断浏览器
    var Sys = {};
    var ua = navigator.userAgent.toLowerCase();
    var s;
    (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] :
        (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
            (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
                (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
                    (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;
    var file_url="";
    if(Sys.ie<="6.0"){
        //ie5.5,ie6.0
        file_url = obj.value;
    }else if(Sys.ie>="7.0"){
        //ie7,ie8
        obj.select();
        obj.blur();
        file_url = document.selection.createRange().text;
    }else if(Sys.firefox){
        //fx
        //file_url = document.getElementById("file").files[0].getAsDataURL();//获取的路径为FF识别的加密字符串
        file_url = readFileFirefox(obj);
    }else if(Sys.chrome){
        file_url = obj.value;
    }
    alert(file_url);
}